﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MultLanguage
{
    public partial class FormMultLangBase : Form
    {
        public FormMultLangBase()
        {
            InitializeComponent();
            MultLanguageManage.Instance.RegForm(this);
        }

        /// <summary>
        /// 对控件应用当前语言
        /// </summary>
        public virtual void ApplyLanguage()
        {
            ComponentResourceManager resources = new ComponentResourceManager(this.GetType());
            ArrayList list = new ArrayList();
            GetChildControls(list, this);
            this.Text = resources.GetString("$this.Text");
            foreach (Control ctl in list)
            {
                resources.ApplyResources(ctl, ctl.Name);
            }
        }

        /// <summary>
        /// 把可以本地化的控件放入LIST
        /// </summary>
        /// <param name="list"></param>
        /// <param name="ctl"></param>
        private void GetChildControls(ArrayList list, Control ctl)
        {
            //容器不可以本地化
            if (!(ctl is ContainerControl))
            {
                list.Add(ctl);
            }
           
            if (!ctl.HasChildren)
            {
                return;
            }

            foreach (Control c in ctl.Controls)
            {
                if (!(c is Form))
                {
                    GetChildControls(list, c);
                }             
            }
        }

    }
}
